﻿#include <iostream>
#include "root_directory.h"

#include "sqlite.hpp"

#include "connection_pool.hpp"
#include "dbng.hpp"

struct person {
  int id;
  std::string name;
  int age;
};
REFLECTION(person, id, name, age)

struct student {
  int id;
  std::string name;
  int age;
};
REFLECTION_WITH_NAME(student, "t_student", id, name, age)

int main()
{

    std::string db = std::string(resources_root) + "/build/bin/test_ormpp.db";

    ormpp::dbng<ormpp::sqlite> sqlite;
    bool ret =  sqlite.connect(db.c_str());
    ret = sqlite.create_datatable<person>(ormpp_auto_key{"id"});
    ret = sqlite.create_datatable<student>(ormpp_auto_key{"id"});

    {
        ret = sqlite.delete_records<person>();
        sqlite.insert<person>({0, "person"});
        sqlite.insert<person>({0, "person", 6});
        auto vec = sqlite.query<person>();
        for (auto &[id, name, age] : vec) {
            std::cout << id << ", " << name << ", " << age << "\n";
        }
    }

    {
        sqlite.delete_records<student>();
        sqlite.insert<student>({0, "student", 1});
        sqlite.insert<student>({0, "student", 2});
        sqlite.insert<student>({0, "student", 3});
        sqlite.insert<student>({0, "student", 3});
        {
            auto vec = sqlite.query<student>("name='student'", "order by age desc");
            for (auto &[id, name, age] : vec) {
                std::cout << id << ", " << name << ", " << age << "\n";
            }
        }
        {
            auto vec = sqlite.query<student>("age=3", "order by id desc", "limit 1");
            for (auto &[id, name, age] : vec) {
                std::cout << id << ", " << name << ", " << age << "\n";
            }
        }
  }

    return 0;
}